package com.spogher.bsc.pages;

import java.io.Serializable;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.data.dao.interfaces.ChampionshipDao;
import com.spogher.bsc.data.dao.interfaces.TeamDao;
import com.spogher.bsc.data.dataobjects.Championship;
import com.spogher.bsc.data.dataobjects.Team;
import com.spogher.bsc.panels.LastResultsPanel;
import com.spogher.components.dropdownChoices.ChampionshipDropDownChoice;
import com.spogher.components.dropdownChoices.TeamDropDownChoice;

public class HomePage extends BscTemplatePage {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private TeamDao teamDao;
	@SpringBean
	private ChampionshipDao championshipDao;
	
	private WebMarkupContainer lastResultsContainer = new WebMarkupContainer("lastResultsContainer");

    public HomePage(final PageParameters parameters) {
    	super();
    	createBodyComponents();
    	
    }
    
    
    private void createBodyComponents() {
    	lastResultsContainer.setOutputMarkupId(true); 
    	lastResultsContainer.add(new LastResultsPanel("lastResultsPanel"));
    	add(lastResultsContainer);
    	
    	final Form<LastResultsFilterModelObject> lastResultsFilter = new Form<LastResultsFilterModelObject>("lastResultsFilter", 
    			new CompoundPropertyModel<LastResultsFilterModelObject>(new LastResultsFilterModelObject()));
    	lastResultsFilter.setOutputMarkupId(true);
    	
    	DropDownChoice<Championship> championshipChoice = new ChampionshipDropDownChoice("championship", championshipDao.findAll());
    	final DropDownChoice<Team> teamChoice = new TeamDropDownChoice("team", teamDao.findByChampionship(null));
    	
    	championshipChoice.add(new AjaxFormSubmitBehavior(lastResultsFilter, "onchange") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				Championship selectedChampioship = lastResultsFilter.getModelObject().getChampionship();
				if (selectedChampioship != null) {
					reloadOnChampionshipSelect(selectedChampioship);
					teamChoice.setChoices(teamDao.findByChampionship(selectedChampioship));
					target.addComponent(lastResultsFilter);
					target.addComponent(lastResultsContainer);
				}				
			}
			
			@Override
			protected void onError(AjaxRequestTarget target) {
				
			}
		});
    	
    	teamChoice.add(new AjaxFormSubmitBehavior(lastResultsFilter, "onchange") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				Team selectedTeam = lastResultsFilter.getModelObject().getTeam();
				if (selectedTeam != null) {
					reloadOnTeamSelect(selectedTeam);
					target.addComponent(lastResultsFilter);
					target.addComponent(lastResultsContainer);
				}				
			}
			
			@Override
			protected void onError(AjaxRequestTarget target) {
				
			}
		});
    	
    	lastResultsFilter.add(championshipChoice);
    	lastResultsFilter.add(teamChoice);
    	add(lastResultsFilter);
    	
	}
    
    private void reloadOnChampionshipSelect(Championship championship) {
    	lastResultsContainer.addOrReplace(new LastResultsPanel("lastResultsPanel", championship));
    }
    
    private void reloadOnTeamSelect(Team team) {
    	lastResultsContainer.addOrReplace(new LastResultsPanel("lastResultsPanel", team));
    }

	private class LastResultsFilterModelObject implements Serializable{

		private static final long serialVersionUID = 1L;
		private Championship championship;
		private Team team;
		public void setChampionship(Championship championship) {
			this.championship = championship;
		}
		public Championship getChampionship() {
			return championship;
		}
		public void setTeam(Team team) {
			this.team = team;
		}
		public Team getTeam() {
			return team;
		}
		
	}
}
